# __init__.py
# 空文件即可
import sys
from pywinauto import Desktop
import re, uiautomation as auto


def click(main_win,title,control_type,found_index=0):
    """
    查找控件并点击
    found_index：查找到多个控件，默认查找第一个控件
    """
    #print(main_win)
    if main_win is None:
        print("main_win不能为空",file=sys.stderr)
        return False
    if title is None:
        print("title不能为空",file=sys.stderr)
        return False
    if control_type is None:
        print("control_type不能为空",file=sys.stderr)
        return False
    if found_index is None:
        found_index = 0

    try:

        if 'Group' == control_type:
            main_win.Exists(3, 0.2)  # 超时等待
            person = auto.GroupControl(searchFromControl=main_win, Name=title,found_index=found_index)
            person.Click()
        else:
          control = main_win.child_window(control_type=control_type, found_index=found_index, title=title)
          control.click_input()
        return True
    except Exception as e:
        print(f'点击错误，没有找到控件，title:{title}，"control_type:{control_type}，found_index:{found_index}，异常信息："{e}')
        return False


